package org.lansir.af.ui;

import org.lansir.af.R;
import org.lansir.af.database.DataRowActivity;
import org.lansir.af.database.DataRowActivityParticipator;
import org.lansir.af.database.DataTable;
import org.lansir.af.database.Database;
import org.lansir.af.dataview.DataViewActivityParticipator;
import org.lansir.af.dataview.DataViewItem;
import org.lansir.af.dataview.DataViewItemActivityParticipator;
import org.lansir.af.util.Utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScheduleActivityInfoFragment extends CommonFragment {
	private TextView tvActivityInfoOrganizer;
	private TextView tvActivityInfoTitle;
	private TextView tvActivityInfoLocation;
	private TextView tvActivityInfoTime;
	private TextView tvActivityInfoRingerMode;
	private TextView tvActivityInfoAlarm;
	private TextView tvActivityInfoRepeat;
	private TextView tvActivityInfoParticipator = null;;
	private LinearLayout llayActivityInfoParticipator;


	private DataRowActivity dataRowActivity;
	private DataTable dataTableActivity;

	private DataViewActivityParticipator dataViewActivityParticipator;

	// 发起人是否是自己
	private boolean bOrganizerMyself = false;	
	private long lRowId;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setHasOptionsMenu(true);
		dataRowActivity = new DataRowActivity(this.userdb);
		dataTableActivity = new DataTable(this.dataRowActivity);
		lRowId = requestedRowId();
		Log.v("ActivityFairy", "ActivityInfo:RowId:" + lRowId);
		dataViewActivityParticipator = new DataViewActivityParticipator(this.userdb, lRowId);
		dataTableActivity.getRowDataForEdit(lRowId);
		bOrganizerMyself = dataRowActivity.getOrganizer().equals(utils.getResStr(R.string.strActivityOrganizerMe));
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View viewRoot = inflater.inflate(R.layout.fragment_schedule_activity_info, null);

		tvActivityInfoOrganizer = (TextView) viewRoot.findViewById(R.id.tvActivityInfoOrganizer);
		tvActivityInfoTitle = (TextView) viewRoot.findViewById(R.id.tvActivityInfoTitle);
		tvActivityInfoLocation = (TextView) viewRoot.findViewById(R.id.tvActivityInfoLocation);

		tvActivityInfoTime = (TextView) viewRoot.findViewById(R.id.tvActivityInfoTime);
		tvActivityInfoRingerMode = (TextView) viewRoot.findViewById(R.id.tvActivityInfoRingerMode);
		tvActivityInfoAlarm = (TextView) viewRoot.findViewById(R.id.tvActivityInfoAlarm);
		tvActivityInfoRepeat = (TextView) viewRoot.findViewById(R.id.tvActivityInfoRepeat);
		

		
		
		if (!bOrganizerMyself) {
			llayActivityInfoParticipator = (LinearLayout) viewRoot.findViewById(R.id.llayActivityInfoParticipator);
			llayActivityInfoParticipator.setVisibility(View.GONE);
		} else {
			llayActivityInfoParticipator = (LinearLayout) viewRoot.findViewById(R.id.llayActivityInfoParticipator);
			llayActivityInfoParticipator.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					bundleOtherDataStartup.putLong(CommonFragment.requestBundleRowId, dataRowActivity.getActivityId());
					bundleOtherDataStartup.putString(CommonFragment.requestBundleActivityTitle, dataRowActivity.getSubject());
					openActivity("org.lansir.af.intent.action.ACTION_MODE_VIEW_ACTIVITYINFO_PARTICIPATORS");
				}
			});
			tvActivityInfoParticipator = (TextView) viewRoot.findViewById(R.id.tvActivityInfoParticipator);
		}


		initStates();

		return viewRoot;
	}

	private void initStates() {
		tvActivityInfoOrganizer.setText(dataRowActivity.getOrganizer());
		tvActivityInfoTitle.setText(dataRowActivity.getSubject());
		tvActivityInfoLocation.setText(dataRowActivity.getLocation());
		// Calendar cal = dataRowActivity.getStartDate();
		// Date date = new
		// Date(dataRowActivity.getStartDate().getTimeInMillis());
		// date.setTime(dataRowActivity.getStartDate().getTimeInMillis());
		tvActivityInfoTime.setText(utils.getMinuteDate(dataRowActivity.getStartDate()));
		tvActivityInfoRingerMode.setText(this.getResources().getStringArray(R.array.ringerMode)[dataRowActivity.getRingerMode()]);
		tvActivityInfoAlarm.setText(this.getResources().getStringArray(R.array.activityInfoAlarm)[dataRowActivity.getAlarm() ? 0 : 1]);
		String strRepeat = String.format(this.getResources().getStringArray(R.array.activityInfoRepeat)[dataRowActivity.getRepeatType()], dataRowActivity
				.getRepeatEvery());
		tvActivityInfoRepeat.setText(strRepeat);
		dataViewActivityParticipator.reloadTable();
		int iReply = 0;
		for (DataViewItem dataViewItem : dataViewActivityParticipator.getRowsForTotal()) {
			DataViewItemActivityParticipator dataItemActivityParticipator = (DataViewItemActivityParticipator) dataViewItem;
			if (dataItemActivityParticipator.getReply() == 0) {
				iReply++;
			}
		}
		if(dataViewActivityParticipator.getRowsCountTotal() == 0){
			llayActivityInfoParticipator.setVisibility(View.GONE);
			return;
		}
		// TODO 待修改添加参与人
		if (bOrganizerMyself)
			tvActivityInfoParticipator.setText("( " + iReply + "/" + dataViewActivityParticipator.getRowsCountTotal() + " )");
	}

	
	private void deleteActivity() {
		//TODO
		/*if (bOrganizerMyself) {
			utils.showSelectDialog(R.string.strActivityInfoMsgDeleteSelfOrganizer, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (sendDeleteMessage()) {
						deleteActivityAndExit();
					}
				}

			}, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteActivityAndExit();
				}

			}, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}

			});
		} else {*/
			utils.showConfirmDialog(R.string.strActivityInfoMsgDelete, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteActivityAndExit();
				}

			}, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}

			});
//		}
	}
	
	/**
	 * 发送删除活动消息 TODO
	 * 
	 * @return
	 */
	private boolean sendDeleteMessage() {
		return true;
	}

	private void deleteActivityAndExit() {
		DataRowActivityParticipator dataRowActivityParticipator = new DataRowActivityParticipator(this.userdb);
		DataTable dataTableActivityParticipator = new DataTable(dataRowActivityParticipator);
		Database.Result result = dataTableActivityParticipator.deleteData("ActivityID=" + dataRowActivity.getActivityId());
		if (result != Database.Result.Success) {
			utils.showMsgResStr(Database.getErrDesc(result), Utils.MSGTYPE_ERROR);
		}
		result = dataTableActivity.deleteData(dataRowActivity.getActivityId());
		if (result != Database.Result.Success) {
			utils.showMsgResStr(Database.getErrDesc(result), Utils.MSGTYPE_ERROR);
		} else {
			closeActivity(dataTableActivity);
		}
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.schedule_activity_info, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.menu_edit:
			if (bOrganizerMyself) {
				bundleOtherDataStartup.putLong(CommonFragment.requestBundleRowId, dataRowActivity.getActivityId());
				openActivityForResult(0, "org.lansir.af.intent.action.ACTION_MODE_EDIT_ACTIVITY");

			} else {
				utils.showMsgResStr(R.string.strActivityInfoMsgCannotModify, Utils.MSGTYPE_ERROR);
			}
			return true;
		case R.id.menu_delete:
			deleteActivity();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == Activity.RESULT_OK) {
			Bundle extras = data.getExtras();

			if (extras != null) {
				bundleOtherDataStartup.putAll(extras);
				setIntentResult("", Activity.RESULT_OK, bundleOtherDataStartup);
				getActivity().finish();
			}
		}

	}
	
	

}
